package com.ddwl.shop.service;

import com.ddwl.common.constant.BaseEnum;
import org.springframework.util.CollectionUtils;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.ShopCollectionBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.shop.ShopCollectionVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.shop.dao.ShopCollectionDao;
import com.ddwl.shop.dao.model.ShopCollection;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShopCollectionService {

    @Resource
    private ShopCollectionDao shopCollectionDao;
    @Resource
    private ShopService shopService;


    @Transactional(rollbackFor = Throwable.class)
    public void create(ShopCollectionBo bo) {
        ParamException.isBlank(bo.getShopId(),"门店id为空");
        ShopCollection shopCollection=new ShopCollection();
        shopCollection.setUserId(bo.getUserId());
        shopCollection.setShopId(bo.getShopId());
        ShopCollection collection = shopCollectionDao.selectOne(shopCollection);
        if (collection!=null){
            collection.setIsEffect(BaseEnum.StatusEnum.ENABLE.getCode());
            shopCollectionDao.updateByPrimaryKeySelective(collection);
        }else{
            ShopCollection shopCollection1 = BeanConverUtils.convertBean(bo, ShopCollection.class);
            shopCollection.setIsEffect(BaseEnum.StatusEnum.ENABLE.getCode());
            shopCollectionDao.insert(shopCollection1);
        }
    }


    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        ParamException.isNull(id,"id为空");
        ShopCollection shopCollection=new ShopCollection();
        shopCollection.setId(id);
        shopCollection.setGmtModified(Instant.now());
        shopCollection.setIsEffect(BaseEnum.StatusEnum.DISABLE.getCode());
        shopCollectionDao.updateByPrimaryKeySelective(shopCollection);
    }

    public PageVo<ShopCollectionVo> page(PageBo boPageBo, String userId) {
        PageHelper.startPage(boPageBo.getPage(),boPageBo.getPageSize(), MySqlUtil.orderBy(boPageBo.getSort(),boPageBo.isAsc()));
        ShopCollection shopCollection=new ShopCollection();
        shopCollection.setIsEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        shopCollection.setUserId(userId);
        PageInfo<ShopCollection> info = new PageInfo(shopCollectionDao.select(shopCollection));
        List<ShopCollectionVo> list = BeanConverUtils.convertBeanList(info.getList(),ShopCollectionVo.class);
        if (!CollectionUtils.isEmpty(list)){
            fillDate(list);
        }
        return new PageVo<>(info.getTotal(),boPageBo.getPage(),boPageBo.getPageSize(),list);
    }

    private void fillDate(List<ShopCollectionVo> list) {
        List<String> idList = list.stream().map(ShopCollectionVo::getShopId).collect(Collectors.toList());
        List<ShopVo> shopVos = shopService.getShopListByIds(idList);
        if (CollectionUtils.isEmpty(shopVos)){
            return;
        }
        shopVos.stream().forEach(o->{
            shopService.setShopCategoryName(o);
            for (ShopCollectionVo shopVo:list){
                if (shopVo.getShopId().equals(o.getId())){
                    BeanUtils.copyProperties(shopVo,o,"id");
                }
            }
        });
    }
}
